$(function(){
	
	$("#filemanager")
	.resizable({
		alsoResize: '#fm_content , #selecter , .thumbs , .selected',
		minWidth:700
	})
	.draggable({handle: "#fm_head"});
	
	var setDel = function(){		
		$('.selected dl').unbind().click(function(){
			var num = Number($('.right > p span').text()) - 1;
			$('.right > p span').text(num);
			$(this)	.remove();
		});
	}
	
	//サムネイルの選択
	$('.thumbs dl').click(function(){
		var src = $('img',this).attr('src');
		var txt = $('dd',this).text();
		var id = $(this).attr('name');
		if($('#' + id).size() >= 0){ //for debug
			var num = Number($('.right > p span').text()) + 1;
			$('.right > p span').text(num);
			var html = '<dl id="' + id + '"><dt><img src="' + src + '" alt=""></dt><dd>' + txt + '</dd></dl>';
			$('.selected').append(html);
			setDel();
		}
	});
	//削除
	setDel();
	
	$('.fm_category a').click(function(){
		if(!$("#megamenu").is(':hidden')){
			$("#megamenu").slideUp(200);
		}else {
			$("#megamenu").slideDown(200);
		}
	});
	
	$("#megamenu a").click(function(){
		$('.fm_category a').html($(this).text());
		$("#megamenu").slideUp(200);
	});
	
	$('.cancel , #filemanager .close_btn').click(function(){
		$("#filemanager").hide();
		$('#modal_background').remove();
	});
	
	$('.ok').click(function(){
		var html = '<p><img src="http://demo.shift-jp.net/web-admin/assets/images/filemanager/sample.gif" alt="" width="700" height="465" data-mce-src="http://demo.shift-jp.net/web-admin/assets/images/filemanager/sample.gif" data-mce-selected="1"></p>';
		var c  = $('#page_text_ifr').contents().find('body');
		c.append(html);
		$('.cancel').click();
	});
	
});